<template>
    <el-container class="sys_parameter" >
        <el-main>
            <el-card class="box-card">
                <el-form ref="systemSettingForm" :rules="rules" :model="systemSettingForm" label-width="150px" >
                    <el-form-item label="登录后显示URL" prop="url_after_logined">
                        <el-input v-model="systemSettingForm.url_after_logined"  type="text"/>
                    </el-form-item>
                    <div>
                        <el-button type="primary" @click="saveSetting" v-btnLoading style="margin-left: 150px;width: 100px;">确 定</el-button>
                    </div>
                </el-form>
            </el-card>
        </el-main>
    </el-container>
</template>

<script>
    import sysSettingApi from "@common/api/system/sysSettingApi";
    import constants from "@common/api/constants";
    import systemSettingApi from "@common/api/system/systemSettingApi";

    export default {
        name: "SystemSetting",
        data () {
            let $this = this;
            return {
                rules: {
                    name: [
                        {required: true, message: '请输入参数名称', trigger: 'blur'},
                        {pattern: /^[A-Za-z0-9\u4e00-\u9fa5]{1,32}$/, message: '最大 32 位大小写英文、数字或者其它地区语言字符，不能包含特殊字符'}
                    ],
                    value: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                        //目前只兼容中文、大小写英文、数字
                        {pattern: /^[A-Za-z0-9+-_.]{1,32}$/, message: '最大32位字符，大小写英文、数字或者以下特殊字符（+-_.），不能包含特殊字符'}
                    ],
                },
                //弹窗option
                dialogOption: {
                    visible: false,
                },
                //编辑新增的form数据
                systemSettingForm: {
                    code:constants.CODE_SYSTEM_SETTING,
                    url_after_logined:"",
                },
                searchValue: '',
            }
        },
        mounted () {
            this.getSystemSetting();
        },
        methods: {
            getSystemSetting () {
                let $this = this;
                let queryData = {code:constants.CODE_SYSTEM_SETTING}
                sysSettingApi.getSysSettingByCodeOwnerId(queryData, result => {
                    if (result.data) {
                        if (result.data.json){
                            $this.systemSettingForm = JSON.parse(result.data.json);
                        }
                    }
                })
            },
            toggleSystem(){
                // console.log(this.systemSettingForm.system_contract);
            },
            //保存/更新设置
            saveSetting () {
                let $this = this;
                $this.$refs['systemSettingForm'].validate(valid => {
                    if (valid) {
                        let setting = $.extend({code:constants.CODE_SYSTEM_SETTING},$this.systemSettingForm,{json:JSON.stringify($this.systemSettingForm)});
                        sysSettingApi.saveSysSetting(setting, () => {
                            $this.$message.success('保存成功！');
                            systemSettingApi.clearSystemSettingCache({},()=>{});
                        })
                    } else {
                        return false;
                    }
                });

            },
        },
    }
</script>

<style lang="less" rel="stylesheet/less" scoped>
    .sys_parameter {
        height: 100%;

        .buttons {
            margin: 0 0 10px 0;
        }
    }
</style>
